simulationFPOP/Test The data without changepoints/Test1,3 Time complexity/Test 3 n = 1000 -10000 Time complexity PELT FPOP.R

############################################################################
############################################################################
##                    package installation                                ##
############################################################################
############################################################################

devtools::install_github("lpishchagina/fpop2D")
library(fpop2D)
devtools::install_github("lpishchagina/OptPartitioning2D")
library(OptPartitioning2D)

############################################################################
############################################################################
##                         Preprocessing                                  ##
############################################################################
############################################################################

###############################
#       Function OneStep      #
###############################

#Function OneStep  returns the execution time of a given algorithm

OneStep <- function(data1, data2, penalty, type, func = "FPOP2D")
{
  if (type == 0){t <- system.time(FPOP2D(data1, data2, penalty, type = 0))[[1]]}
  if (type == 2){t <- system.time(FPOP2D(data1, data2, penalty, type = 2))[[1]]}
  return(t)
}


#############################################################################################
#############################################################################################
## Test3: Time complexity (the plot of the mean running time with respect to data length). ##
##          linear growth n,  the data without changepoints                                ##
#############################################################################################
#############################################################################################

###############################
#    data length genetation   #
###############################
nStep <- 10  
vect_n <- seq(from = 1000, to = 10000, length.out = nStep)
vect_n 
#[1]  1000  2000  3000  4000  5000  6000  7000  8000  9000 10000
nRep <- 10


###############################
#            tables           #
###############################

###################### PELT ##################################################
resPELT <- data.frame(matrix(0, nStep, nRep + 1))
colnames(resPELT) <- c("n", paste0("Rep", 1:nRep))

###################### FPOP type = 2 #########################################
resFPOP2Dt2 <- data.frame(matrix(0, nStep, nRep + 1))
colnames(resFPOP2Dt2) <- c("n", paste0("Rep", 1:nRep))


###############################
#       tables filling        #
###############################

sigma <- 1

for(i in 1:length(vect_n)){
  chp <- c(vect_n[i])
  mu1 <- c(10)
  mu2 <- c(5)
  penalty <- 2 * sigma * log(vect_n[i])
  
  data <- data_gen2D(vect_n[i], chp, mu1, mu2, sigma)
  resPELT[i,] <- c(vect_n[i], replicate(nRep, OneStep(data[1,], data[2,], penalty, type = 0, func = "FPOP2D")))
  resFPOP2Dt2[i,] <- c(vect_n[i], replicate(nRep, OneStep(data[1,], data[2,], penalty, type = 2, func = "FPOP2D")))
}

###################### PELT ##################################################
resPELT
# 
#n Rep1 Rep2 Rep3 Rep4 Rep5 Rep6 Rep7 Rep8 Rep9 Rep10
#1   1000 0.05 0.05 0.03 0.03 0.03 0.03 0.03 0.03 0.03  0.03
#2   2000 0.44 0.43 0.36 0.42 0.44 0.43 0.40 0.40 0.37  0.41
#3   3000 0.25 0.23 0.24 0.25 0.25 0.25 0.25 0.22 0.22  0.22
#4   4000 0.75 0.74 0.75 0.72 0.75 0.68 0.70 0.70 0.77  0.76
#5   5000 1.87 1.86 1.87 1.86 1.88 1.84 1.92 1.94 1.98  1.92
#6   6000 2.05 1.97 2.05 2.04 2.00 1.95 1.99 2.05 1.97  2.03
#7   7000 1.95 2.02 2.00 2.01 2.00 2.06 2.02 2.01 2.07  2.03
#8   8000 3.26 3.24 3.25 3.21 3.29 3.31 3.18 3.28 3.24  3.31
#9   9000 4.76 4.64 4.75 4.69 4.84 4.84 4.78 4.72 4.89  4.78
#10 10000 6.59 6.62 6.69 6.61 6.61 6.65 6.66 6.55 6.56  6.54

###################### FPOP type = 2 #########################################
resFPOP2Dt2 
#n Rep1 Rep2 Rep3 Rep4 Rep5 Rep6 Rep7 Rep8 Rep9 Rep10
#1   1000 0.01 0.00 0.01 0.02 0.00 0.02 0.02 0.01 0.02  0.02
#2   2000 0.04 0.05 0.07 0.06 0.06 0.07 0.05 0.05 0.06  0.07
#3   3000 0.07 0.05 0.05 0.06 0.05 0.04 0.05 0.05 0.05  0.04
#4   4000 0.18 0.17 0.16 0.18 0.16 0.15 0.16 0.15 0.16  0.17
#5   5000 0.14 0.14 0.14 0.14 0.13 0.12 0.12 0.14 0.14  0.14
#6   6000 0.17 0.17 0.17 0.18 0.17 0.17 0.18 0.17 0.15  0.17
#7   7000 0.18 0.17 0.19 0.18 0.18 0.17 0.19 0.18 0.19  0.17
#8   8000 0.22 0.22 0.22 0.21 0.21 0.22 0.22 0.22 0.22  0.21
#9   9000 0.27 0.28 0.28 0.28 0.28 0.28 0.28 0.28 0.28  0.28
#10 10000 0.31 0.31 0.31 0.31 0.31 0.31 0.31 0.32 0.32  0.32


###############################
#   mean time in second       #
###############################


###################### PELT ##################################################
mean_PELT <- rowMeans(resPELT[,-1])
#[1] 0.034 0.410 0.238 0.732 1.894 2.010 2.017 3.257 4.769 6.608
plot(vect_n, mean_PELT, xlab = "data length", ylab = "mean time in second",  main = "Time complexity of PELT", col = "steelblue")
lines(vect_n, mean_PELT, col="steelblue")

###################### FPOP type = 2 #########################################
mean_resFPOP2Dt2 <- rowMeans(resFPOP2Dt2[,-1])
#
plot(vect_n, mean_resFPOP2Dt2, xlab = "data length", ylab = "mean time in second",  main = "Time complexity of FPOP(type = 2)", col = "forestgreen")
lines(vect_n, mean_resFPOP2Dt2, col= "forestgreen")


###############################
#  plots: mean time in second #
###############################


###################### plot PELT ##############################################
plot(vect_n,mean_PELT, col = "steelblue", xlab = "data length", ylab = "mean time in second",  main = "Time complexity")
lines(vect_n, mean_PELT, col = "steelblue")


###################### plot FPOP type = 2 #####################################
points(vect_n, mean_resFPOP2Dt2, col = "purple4")
lines(vect_n, mean_resFPOP2Dt2, col = "purple4")



############################### legend ########################################
location = "topleft"
labels = c("PELT",  "FPOP(type = 2)")
colors = c("steelblue", "purple4")
legend(location, labels, fill = colors)

#############################################################################################
#############################################################################################
##                                        End Test3                                        ##
#############################################################################################
#############################################################################################
lpishchagina/fpop2D documentation built on March 14, 2021, 11:05 p.m.